package calmAnnotation.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import jenamuseum.annotation.Annotation;

import calmAnnotation.model.CALMAnnotationManagerLauncher;

public  class MainFrame extends JFrame{
	public  JTable annotationTable;
	public JScrollPane jsp;
	public static String[] column = {"Nom", "Date", "Auteur", "Corps"};
	public JTextField searchField;
	public JButton searchButton;
	public JPanel navigationPanel;
	public JPanel searchViewPanel;
	public JButton addAnnotation;
	public JButton deleteAnnotation;
	public JPanel searchPanel;
	public JPanel annotationPanel;
	public JLabel author;
	public JLabel artwork;
	public JLabel title;
	public JLabel body;
	public JLabel date;
	public JTextField authorText;
	public JTextField titleText;
	public JTextField artworkText;
	public JTextField dateText;
	public JTextArea bodyText;
	public JButton changeAnnotation;
	public JButton newAnnotation;
	public int selectedRow;
	
	
	
	public MainFrame()
	{
	
		
		super("CALM Annotation Manager");
		annotationTable=new JTable(CALMAnnotationManagerLauncher.getAppModel().mesAnnotations){
		    @Override
		    public boolean isCellEditable(int rowIndex, int columnIndex) {
		        
		        //ici la cellule (1, 2) est non-editable
		        if (columnIndex<6) return false;
		        
		        //le reste est editable
		        return true;
		    }
		};
		annotationTable.getColumn("Button").setCellRenderer(new ButtonRenderer());
		annotationTable.setAutoCreateRowSorter(true);
		annotationTable.getColumn("Button").setCellEditor((new ButtonEditor(new JCheckBox())
		));
        annotationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     //   annotationTable.setCellSelectionEnabled(false);
        //row selection listener:
        ListSelectionListener rowSelectionListener = new ListSelectionListener() {
 
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting() && !CALMAnnotationManagerLauncher.getAppModel().isUpdating) {
                    ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                    selectedRow = lsm.getAnchorSelectionIndex();
                    System.out.println("Selected row: " +  annotationTable.getValueAt(selectedRow, 0));
                    artworkText.setText(annotationTable.getValueAt(selectedRow, 1).toString());
                    authorText.setText(annotationTable.getValueAt(selectedRow, 2).toString());
                    titleText.setText(annotationTable.getValueAt(selectedRow, 3).toString());
                    bodyText.setText(annotationTable.getValueAt(selectedRow, 4).toString());
                    dateText.setText(annotationTable.getValueAt(selectedRow, 5
                    		).toString());


                }
            }
        };
        annotationTable.getSelectionModel().addListSelectionListener(rowSelectionListener);	
        System.out.println("Annotation row = " + CALMAnnotationManagerLauncher.getAppModel().mesAnnotations.getRowCount());
		System.out.println("Annotation column = " + CALMAnnotationManagerLauncher.getAppModel().mesAnnotations.getColumnCount());
		this.setLayout(new BorderLayout());

		jsp = new JScrollPane(annotationTable);		
		jsp.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Liste des annotations", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
		this.add(initSearchViewPanel(),BorderLayout.NORTH);

		this.add(jsp, BorderLayout.CENTER);
		this.setExtendedState(this.MAXIMIZED_BOTH);  
		this.pack();
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
	}
	
	
	public JPanel initSearchPanel()
	{
		searchPanel= new JPanel();
		searchPanel.setLayout(new GridBagLayout());
		searchField = new JTextField("test");
		searchField.setPreferredSize(new Dimension(10,20));
		searchButton = new JButton("Rechercher");
		GridBagConstraints c1 = new GridBagConstraints();
		c1.fill = GridBagConstraints.HORIZONTAL;
		c1.anchor=GridBagConstraints.NORTHEAST;
		c1.gridx = 1;
		c1.gridy = 0;
		c1.weightx=1;
		c1.gridwidth=1;

		searchPanel.add(searchField, c1);
		c1.weightx=0;
		c1.gridx=2;
		c1.insets = new Insets(0, 0, 0, 0);

		searchPanel.add(searchButton, c1);
		searchButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			try {
				CALMAnnotationManagerLauncher.getAppModel().searchByKeyword(searchField.getText());
				System.out.println("SearchField is " + searchField.getText());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			}
		});
		searchPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Rechercher", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
		return searchPanel;
	}

	
	public JPanel initViewAnnotationPanel()
	{
		annotationPanel = new JPanel();
		annotationPanel.setLayout(new GridBagLayout());
		author = new JLabel("Auteur");
		title = new JLabel("Titre de l'annotation");
		artwork = new JLabel("Oeuvre");
		date = new JLabel("Date");
		body = new JLabel("Corps de l'annotation");
		authorText= new JTextField("Auteur");
		//authorText.setBackground(new Color(253));
		authorText.setOpaque(true);
		Dimension d = authorText.getPreferredSize();
		d.width=100;
		authorText.setPreferredSize(d);
		//	authorText.setPreferredSize(d);
		artworkText= new JTextField("Oeuvre");
		//artworkText.setBackground(new Color(253));
		artworkText.setOpaque(true);
		artworkText.setPreferredSize(d);
		//artworkText.setEditable(false);
		
		dateText= new JTextField("Date");
		//dateText.setBackground(new Color(253));
		dateText.setOpaque(true);
		dateText.setPreferredSize(d);
		dateText.setEditable(false);

		d.width=190;
		titleText= new JTextField("Titre");
		titleText.setPreferredSize(d);
		//titleText.setBackground(new Color(253));
		titleText.setOpaque(true);

		bodyText = new JTextArea("Corps de l'annotation");
		bodyText.setLineWrap(true);
		bodyText.setWrapStyleWord(true);
		bodyText.setBorder( titleText.getBorder());
		
		//bodyText.setBackground(new Color(253));
		bodyText.setOpaque(true);
		bodyText.setRows(2);
		bodyText.setPreferredSize(new Dimension(200,10));

		GridBagConstraints c1 = new GridBagConstraints();
		//c1.fill = GridBagConstraints.EAST;
		c1.gridx = 0;
		c1.gridy = 0;
		c1.weightx=0;
		annotationPanel.add(author,c1);
		c1.insets = new Insets(20, 0, 0, 0);

		c1.gridy=1;
		annotationPanel.add(artwork,c1);
		c1.gridy=2;
		annotationPanel.add(date,c1);
		c1.weightx=0;
		//c1.gridheight=GridBagConstraints.RELATIVE;
		c1.anchor=GridBagConstraints.WEST;
		c1.insets = new Insets(0, 10, 0, 0);

		c1.gridx=1;
		c1.gridy=0;
		annotationPanel.add(authorText,c1);
		c1.insets = new Insets(20, 10, 0, 0);

		c1.gridy=1;
		annotationPanel.add(artworkText,c1);
		c1.gridy=2;
		annotationPanel.add(dateText,c1);
		annotationPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Annotation", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
		//c1.fill=GridBagConstraints.WEST;
		c1.gridx=2;
		c1.gridy=0;
		c1.weightx=0;
		c1.insets = new Insets(0, 10, 0, 0);
		
		annotationPanel.add(title,c1);
		c1.insets = new Insets(20, 10, 0, 0);

		c1.gridy=1;
		annotationPanel.add(body,c1);
		c1.gridy=2;
		changeAnnotation= new JButton("Modifier");
		changeAnnotation.addActionListener( new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					CALMAnnotationManagerLauncher.getAppModel()
					.updateAnnotation(new Annotation(((Integer)annotationTable.getValueAt(selectedRow, 0)), titleText.getText(), artworkText.getText(), bodyText.getText(), authorText.getText(), date.getText()));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});

		annotationPanel.add(changeAnnotation,c1);

		c1.insets = new Insets(0, 10, 0, 0);
		c1.gridx=3;
		c1.gridy=0;
		c1.weightx=1;

		annotationPanel.add(titleText,c1);
		c1.gridy=1;
		c1.insets = new Insets(30, 10, 0, 0);

		annotationPanel.add(bodyText,c1);

		
		c1.gridx=3;
		c1.gridy=2;
		newAnnotation = new JButton("Nouvelle annotation");
		newAnnotation.addActionListener( new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					CALMAnnotationManagerLauncher.getAppModel()
					.insertAnnotation(new Annotation(0, titleText.getText(), artworkText.getText(), bodyText.getText(), authorText.getText(), date.getText()));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		annotationPanel.add(newAnnotation,c1);
		return annotationPanel;
	}
	
	 
	
	public JPanel initSearchViewPanel()
	{
	searchViewPanel=new JPanel();
	GridLayout myFlow = new GridLayout(0,3);
	searchViewPanel.setLayout(myFlow);
	searchViewPanel.add(initViewAnnotationPanel());
	searchViewPanel.add(initSearchPanel());
	return searchViewPanel;
	}
}
